﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.Shell.Interop;
using Microsoft.VisualStudio;
using Microsoft.VisualStudio.VCProject;
using Microsoft.VisualStudio.VCProjectEngine;
using EnvDTE;

namespace HandOfSorrow.VsHelper
{
    class SolutionListener : IVsSolutionEvents3
    {
        private DTE _dte;
        private VCProjectHelper _helper;
        public SolutionListener(EnvDTE.DTE dte)
        {
            _dte = dte;
            _helper = new VCProjectHelper();
        }

        public int OnAfterCloseSolution(object pUnkReserved)
        {
            return VSConstants.S_OK;
        }
        public int OnAfterClosingChildren(IVsHierarchy pHierarchy)
        {
            return VSConstants.S_OK;
        }
        public int OnAfterLoadProject(IVsHierarchy pStubHierarchy, IVsHierarchy pRealHierarchy)
        {
            return VSConstants.S_OK;
        }
        public int OnAfterMergeSolution(object pUnkReserved)
        {
            return VSConstants.S_OK;
        }
        public int OnAfterOpeningChildren(IVsHierarchy pHierarchy)
        {
            return VSConstants.S_OK;
        }
        public int OnAfterOpenProject(IVsHierarchy pHierarchy, int fAdded)
        {
       //     if (1 == fAdded)
        //    {
                Solution sln = _dte.Solution;
                _helper.ConfiguringVCProject(pHierarchy);
                uint cookie;
                pHierarchy.AdviseHierarchyEvents(new HierarchyEvents(), out cookie);
        //    }
            return VSConstants.S_OK;
        }
        public int OnAfterOpenSolution(object pUnkReserved, int fNewSolution)
        {
            Solution sln = _dte.Solution;
            if (null != sln)
            {
                foreach (Project prj in sln.Projects)
                {
                    VCProject vc_prj = prj.Object as VCProject;
                    if (null == vc_prj)
                        continue;

                 //   _helper.ListenVCProjectEvent(vc_prj);
                }
            }
            return VSConstants.S_OK;
        }
        public int OnBeforeCloseProject(IVsHierarchy pHierarchy, int fRemoved)
        {
            return VSConstants.S_OK;
        }
        public int OnBeforeCloseSolution(object pUnkReserved)
        {
            return VSConstants.S_OK;
        }
        public int OnBeforeClosingChildren(IVsHierarchy pHierarchy)
        {
            return VSConstants.S_OK;
        }
        public int OnBeforeOpeningChildren(IVsHierarchy pHierarchy)
        {
            return VSConstants.S_OK;
        }
        public int OnBeforeUnloadProject(IVsHierarchy pRealHierarchy, IVsHierarchy pStubHierarchy)
        {
            return VSConstants.S_OK;
        }
        public int OnQueryCloseProject(IVsHierarchy pHierarchy, int fRemoving, ref int pfCancel)
        {
            return VSConstants.S_OK;
        }
        public int OnQueryCloseSolution(object pUnkReserved, ref int pfCancel)
        {
            return VSConstants.S_OK;
        }
        public int OnQueryUnloadProject(IVsHierarchy pRealHierarchy, ref int pfCancel)
        {
            return VSConstants.S_OK;
        }
    }
    class HierarchyEvents : IVsHierarchyEvents
    {
        public int OnInvalidateIcon(IntPtr hicon)
        {
            return VSConstants.S_OK;
        }
        public int OnInvalidateItems(uint itemidParent)
        {
            return VSConstants.S_OK;
        }
        public int OnItemAdded(uint itemidParent, uint itemidSiblingPrev, uint itemidAdded)
        {
            return VSConstants.S_OK;
        }
        public int OnItemDeleted(uint itemid)
        {
            return VSConstants.S_OK;
        }
        public int OnItemsAppended(uint itemidParent)
        {
            return VSConstants.S_OK;
        }
        public int OnPropertyChanged(uint itemid, int propid, uint flags)
        {
            return VSConstants.S_OK;
        }

    }
}
